﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MrPainter
{
    class Player : GameObject
    {
        public Vector2 velocity;          // Velocidade com que o jogador se move.
        public Vector3 center;        // Centro do jogador
        public Vector3 lastPosition;  // Última posição do jogador
        public int ink = 10;          // Quantidade de tinta que o jogador possui
        public double radius;         // Raio do jogador
        public bool gravity = false;

        public Player(Texture2D texture, GameWindow Window, Vector2 position, int velocity)
            : base(texture, Window)
        {
            this.position = position;
            this.velocity = new Vector2(velocity,velocity);
            this.center = new Vector3(this.position.X + (float)this.radius, this.position.Y + (float)this.radius, 0);
            this.lastPosition = new Vector3(this.position.X + (float)this.radius, this.position.Y + (float)this.radius, 0);
            this.radius = this.texture.Width / 2;
        }

        public override void Update(GameTime gameTime)
        {
            // Método herdado de GameObject, temos método overloading (dois métodos com mesmo nome e parâmetros diferentes)
        }

        public void Update(GameTime gameTime, KeyboardState keyboard, MouseState mouse)
        {
            lastPosition = center;

            if (gravity)
            {
                if (velocity.Y < 15.0f)
                    velocity.Y += 0.2f;

                this.position.X += velocity.X;
                this.position.Y += velocity.Y;
            }

            if (keyboard.IsKeyDown(Keys.Right))
            {
                if ((this.position.X + velocity.X) >= (Window.ClientBounds.Width - texture.Width))
                {
                    this.position.X = Window.ClientBounds.Width - texture.Width;
                }
                else
                {
                    this.position.X += velocity.X;
                }
            }

            if (keyboard.IsKeyDown(Keys.Left))
            {
                if ((this.position.X - velocity.X) <= 0)
                {
                    this.position.X = 0;
                }
                else
                {
                    this.position.X -= velocity.X;
                }
            }

            if (keyboard.IsKeyDown(Keys.Up))
            {
                if ((this.position.Y - velocity.Y) <= 0)
                {
                    this.position.Y = 0;
                }
                else
                {
                    this.position.Y -= velocity.Y;
                }
            }

            if (keyboard.IsKeyDown(Keys.Down))
            {
                if ((this.position.Y + velocity.Y) >= (Window.ClientBounds.Height - texture.Height))
                {
                    this.position.Y = Window.ClientBounds.Height - texture.Height;
                }
                else
                {
                    this.position.Y += velocity.Y;
                }
            }

            if (keyboard.IsKeyDown(Keys.G))
            {
                gravity = true;
                velocity.X = 0f;
            }

            if (keyboard.IsKeyDown(Keys.N))
            {
                gravity = false;
                velocity.X = 3.0f;
                velocity.Y = 3.0f;
            }

            this.center.X = this.position.X + (float)this.radius;
            this.center.Y = this.position.Y + (float)this.radius;
        }
    }
}
